package com.tywj.lib.core.widget.recycleview.adapter

import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.autocareai.lib.list.ObservableArrayList

import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.lib.core.common.extension.onClick

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/13
 *     desc   : RecyclerView基类适配器，主要是为了封装第三方框架，方便替换
 *     version: 1.0.0
 * </pre>
 */
abstract class BaseAdapter<T> : BaseQuickAdapter<T, BaseViewHolder> {

    /**
     * itemView点击事件是否限定时间之内只取第一个点击事件，防止重复点击，默认限定时间只取第一个点击事件。
     */
    private var isOnItemClickWithInterval = true

    constructor() : super(0, null)

    constructor(@LayoutRes layoutResId: Int) : super(layoutResId, null)

    constructor(data: List<T>?) : super(0, data)

    constructor(@LayoutRes layoutResId: Int, data: List<T>?) : super(layoutResId, data)

    override fun setNewData(data: List<T>?) {
        super.setNewData(data)

        if (data != null && data is ObservableArrayList) {
            data.setOnListChangedListener(object :
                ObservableArrayList.OnListChangedListener<ObservableArrayList<T>> {
                override fun onChanged(sender: ObservableArrayList<T>) {
                    notifyDataSetChanged()
                }

                override fun onItemRangeChanged(
                    sender: ObservableArrayList<T>,
                    positionStart: Int,
                    itemCount: Int
                ) {
                    notifyItemRangeChanged(positionStart + headerLayoutCount, itemCount)
                }

                override fun onItemRangeInserted(
                    sender: ObservableArrayList<T>,
                    positionStart: Int,
                    itemCount: Int
                ) {
                    notifyItemRangeInserted(positionStart + headerLayoutCount, itemCount)
                }

                override fun onItemMoved(
                    sender: ObservableArrayList<T>,
                    fromPosition: Int,
                    toPosition: Int
                ) {
                    notifyItemMoved(
                        fromPosition + headerLayoutCount,
                        toPosition + headerLayoutCount
                    )
                }

                override fun onItemRangeRemoved(
                    sender: ObservableArrayList<T>,
                    positionStart: Int,
                    itemCount: Int
                ) {
                    notifyItemRangeRemoved(positionStart + headerLayoutCount, itemCount)
                }

            })
        }
    }

    override fun convert(helper: BaseViewHolder, item: T) {}

    /**
     * 重写了item点击事件，加上了防止重复点击
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val baseViewHolder = super.onCreateViewHolder(parent, viewType)
        if (viewType != LOADING_VIEW && viewType != HEADER_VIEW && viewType != EMPTY_VIEW && viewType != FOOTER_VIEW) {
            if (onItemClickListener != null && isOnItemClickWithInterval) {
                baseViewHolder.itemView.onClick {
                    setOnItemClick(
                        baseViewHolder.itemView,
                        baseViewHolder.layoutPosition - headerLayoutCount
                    )
                }
            }
        }

        return baseViewHolder
    }

    override fun onBindViewHolder(
        holder: BaseViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            handleLocalRefresh(holder, mData[holder.layoutPosition - headerLayoutCount], payloads)
        }
    }

    /**
     * itemView添加点击监听，限定时间之内只取第一个点击事件，防止重复点击
     * 注意：[onItemClick]和[onItemClickWithoutInterval]同时只能有一个生效，以后添加的为准
     */
    @Suppress("UNCHECKED_CAST")
    fun onItemClick(listener: (adapter: BaseAdapter<T>, view: View, position: Int) -> Unit) {
        isOnItemClickWithInterval = true
        setOnItemClickListener { adapter, view, position ->
            listener.invoke(adapter as BaseAdapter<T>, view, position)
        }
    }

    /**
     * itemView添加点击监听
     * 注意：[onItemClick]和[onItemClickWithoutInterval]同时只能有一个生效，以后添加的为准
     */
    @Suppress("UNCHECKED_CAST")
    fun onItemClickWithoutInterval(listener: (adapter: BaseAdapter<T>, view: View, position: Int) -> Unit) {
        isOnItemClickWithInterval = false
        setOnItemClickListener { adapter, view, position ->
            listener.invoke(adapter as BaseAdapter<T>, view, position)
        }
    }

    protected open fun handleLocalRefresh(
        helper: BaseViewHolder,
        item: T,
        payloads: MutableList<Any>
    ) {
    }
}